<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加礼品包</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/temp/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/temp/css/frame.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>           
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加礼品包</legend>
</fieldset>

<form class="layui-form layui-form-pane" action="" lay-filter="example" style="padding: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">所属店铺</label>
        <div class="layui-input-inline" style="width: 40%">
            <select lay-filter="admin_id" name="admin_id">
                <option value="">-- 请选择店铺 --</option>
                {foreach $shop_list as $k => $v}
                <option value="{$v.id}">{$v.nickname}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入名称" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 长度1-30个字符
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="number" name="price" lay-verify="price" autocomplete="off" placeholder="请输入价格" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="display: flex;">
        <label class="layui-form-label">缩略图</label>
        <div class="layui-input-inline">
            <div class="fup_files">
                <input type="file" name="small_img" onchange="fuploads($(this))">
                <img src="" class="img" name="img" width="100%" height="100%" style="display: none" />
            </div>
        </div>
    </div>

    <div class="layui-form-item" pane="" style="width: 47.5%">
        <label class="layui-form-label">普通礼品</label>
        <div class="layui-input-block" id="goods_id"></div>
    </div>

    <div class="layui-form-item" pane="" style="width: 47.5%">
        <label class="layui-form-label">次卡礼品</label>
        <div class="layui-input-block" id="card_id"></div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <a href="javascript:history.back(-1)" class="layui-btn layui-btn-primary">返回</a>
        </div>
    </div>
</form>
          
<script src="/static/temp/layui/layui.js" charset="utf-8"></script>

<script>
    //图片上传
    function fuploads(f){
        var nameAll = f[0].files[0].type;
        var name = nameAll.split("/");
        
        if(name[0] != 'image'){
            layer.msg('上传类型错误', {icon: 2});
        }else{
            var url = window.URL.createObjectURL(f[0].files[0]);  // 得到bolb对象路径，可当成普通的文件路径一样使用
            f.next("img").css('display', 'block');
            f.next("img").attr('src', url);
        }
    }

    layui.use(['form', 'layedit'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit;

        // 监听提交
        form.on('submit(submit)', function(data){
            let card_id  = '';
            let goods_id = '';

            $("input[name='card_id']:checked").each(function(){
                card_id += $(this).val() + ','
            })

            $("input[name='goods_id']:checked").each(function(){
                goods_id += $(this).val() + ','
            })

            let formData = new FormData();
            let img = $("input[name='small_img");

            formData.append('admin_id', data.field.admin_id);
            formData.append('name', data.field.name);
            formData.append('price', data.field.price);
            formData.append('small_img', img[0].files[0]);
            formData.append('card_id', card_id);
            formData.append('goods_id', goods_id);

            // 加载中样式... 
            let loading = layer.msg('正在添加，请稍后...', {icon: 16, shade: 0.3, time:0});

            $.ajax({
                url:'{:url("Coupon/gift_add_data")}',
                data: formData,
                contentType: false,  // 必须false才会自动加上正确的Content-Type
                processData: false,
                type:"post",
                dataType:'json',
                success:function(res){
                    layer.close(loading);
                    if(res.code == 0){
                        layer.msg(res.msg, {icon: 1,time: 1500}, function(){
                            window.location.href = "{:url('Coupon/gift_list')}";
                        });
                    }else{
                        layer.msg('添加失败', {icon: 2});
                    }
                },
                error:function(res){
                    layer.msg(res.msg, {icon: 2});
                }
            })

            return false;
        });

        // 下拉框事件
        form.on('select(admin_id)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象

            $.ajax({
                type: 'post',
                url: '{:url("Coupon/gifts")}',
                data: {admin_id:data.value},
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        $("#goods_id").html('');
                        $("#card_id").html('');

                        res.data.goods_list.forEach(val => {
                            $("#goods_id").append(`<input type="checkbox" name="goods_id" value="${val.id}" title="${val.title}">`)
                        })

                        res.data.card_list.forEach(val => {
                            $("#card_id").append(`<input type="checkbox" name="card_id" value="${val.id}" title="${val.name}">`)
                        })

                        form.render()
                    }else{
                        layer.msg('数据错误', {icon: 2});
                    }
                },error:function(msg){
                    layer.msg('数据错误', {icon: 2});
                }
            })
        });
    });
</script>
</body>
</html>